#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年4月17日

@author: Administrator
'''

import datetime

from django.contrib.auth.models import User

from message.tasks import send_email_message
from message.models import Message, EmailMessage
from message import settings
from message.utils import data_cache

def fetch_user_unreaded_message(user_id):
    u'''
    function: 获取用户未读消息数, 分别包括总数，系统通知，私信，普通消息
    
    user_id - 用户id
    
    return: 例子{'total': 10, 'system': 5, 'normal': 2, 'letter': 3}
    '''
    
    data = data_cache.get_user_message_cache(user_id)
    if data:
        return data
    
    system_count = Message.objects.filter(recv_user=user_id,
                                          state=settings.StateEnum.VALID,
                                          readed=False,
                                          category=settings.MessageCategoryEnum.SYSTEM).count()
    normal_count = 0
    letter_count = 0
    total_count = system_count + normal_count + letter_count
    data = {
        'total': total_count,
        'system': system_count,
        'letter': letter_count,
        'normal': normal_count,
    }
    
    data_cache.set_user_message_cache(user_id, data)
    
    return data
    

def create_email_message(email, subject, content, user_id=None, send=True):
    u'''
    function: 创建并发送email消息
    
    email - 收件人email
    subject - 邮件主题
    content - 邮件内容
    send - 创建后是否发送，默认为创建后发送
    '''
    
    message = EmailMessage.objects.create(email=email,
                                          subject=subject,
                                          content=content,
                                          recv_user=user_id)
    
    if send:
        send_email_message.delay(message.id)

def create_message(send_user_id, recv_user_id, category, content):
    u'''
    function: 创建站内消息
    
    send_user_id - 发送用户id
    recv_user_id - 接收用户id
    category - 消息分类(settings.MessageCategoryEnum)
    content - 消息内容
    
    return: True/False
    '''
    
    message = Message.objects.create(send_user_id=send_user_id,
                           recv_user_id=recv_user_id,
                           category=category,
                           content=content)
    
    data_cache.delete_user_message_cache(message.recv_user_id)
    
    return True

def delete_message(id, user_id=None):
    u'''
    function: 删除站内消息
    
    id - 消息id
    user_id - 是否校验该id为指定用户user_id的
    '''
    
    filter_fields = {'id': id, 'state': settings.StateEnum.VALID}
    if user_id:
        filter_fields['recv_user'] = user_id
    try:
        message = Message.objects.only('id').get(**filter_fields)
    except Message.DoesNotExist:
        return False
    
    Message.objects.filter(id=message.id).update(state=settings.StateEnum.DELETED, modify_time=datetime.datetime.now())
    data_cache.delete_user_message_cache(user_id)
    
    return True

def create_system_message(user_id, content):
    u'''
    function: 创建站内系统消息
    
    user_id - 接收用户id
    content - 消息内容
    
    return: True/False
    '''
    
    system_user = User.objects.only('id').get(username=settings.SYSTEM_MESSAGE_USER)
    
    return create_message(send_user_id=system_user.id,
                          recv_user_id=user_id,
                          category=settings.MessageCategoryEnum.SYSTEM,
                          content=content)
